home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 16 / AMIGAplus Sonderheft 16 (1998)(ICP)(DE)[!].iso / pd / anwendungen / xpk_source / test / testchunkypack.c < prev    next >
C/C++ Source or Header  |  1998-08-27  |  4KB  |  157 lines

  1. #define NAME        "testChunkyPack"
  2. #define DISTRIBUTION    "(Freeware) "
  3. #define REVISION    "0"
  4.  
  5. /* Programmheader
  6.  
  7.     Name:        testChunkyPack
  8.     Author:        SDI
  9.     Distribution:    Freeware
  10.     Description:    tests chunky packing using XpkWrite
  11.     Compileropts:    -
  12.     Linkeropts:    -l xpkmaster amiga
  13.  
  14.  1.0   26.08.98 : first version
  15. */
  16.  
  17. /* The working method of this program is useful especially for games, as it
  18. is designed mainly to store internal structures part for part into an file.
  19.  
  20. For correct working you need to use at least xpkmaster.library 4.34! Either
  21. ensure, that this library is installed, or check after opening
  22.   if(XpkBase->lib_Version > 4 || XpkBase->lib_Revision >= 34)
  23.   { ... do the work ... }
  24.   else
  25.   { ... do some error stuff ... }
  26. */
  27.  
  28. #include <proto/exec.h>
  29. #include <proto/dos.h>
  30. #include <proto/xpkmaster.h>
  31. #include <exec/memory.h>
  32. #include <exec/execbase.h>
  33. #include "SDI_defines.h"
  34.  
  35. struct Library        *XpkBase        = 0;
  36. ULONG            DosVersion        = 37;
  37.  
  38. #define CHUNKSIZE    10240
  39. /* NOTE: this chunksize define is the value passed to XpkOpen. It is NOT
  40. the value, which is used, as xpk may change this internally depending on
  41. the packing algorithm, so we need to check xfib->xf_NLen value to get
  42. buffer size! */
  43.  
  44. /* USER modus cannot be used with this system, as we have no input data
  45. ready, when we start crunching. */
  46.  
  47. #define PARAM "TO/A,METHOD"
  48.  
  49. struct Args {
  50.   STRPTR to;
  51.   STRPTR method;
  52. };
  53.  
  54. struct BufData {
  55.   STRPTR       buffer;
  56.   ULONG         filled;
  57.   LONG         err;
  58.   struct XpkFib *xfib;
  59. };
  60.  
  61. extern struct DosLibrary *DOSBase;
  62. extern struct ExecBase *  SysBase;
  63.  
  64. LONG SaveData(struct BufData *b, APTR buf, ULONG size);
  65.  
  66. void main(void)
  67. {
  68.   struct Args args = {0,"NUKE"}; /* defaults */
  69.   struct RDArgs *rda;
  70.  
  71.   if((rda = ReadArgs(PARAM, (LONG *) &args, 0)))
  72.   {
  73.     if((XpkBase = OpenLibrary(XPKNAME, 4)))
  74.     {
  75.       struct BufData b;
  76.       LONG err;
  77.  
  78.       if(!(err = XpkOpenTags(&b.xfib, XPK_PackMethod, args.method,
  79.       XPK_OutName, args.to, XPK_ChunkSize, CHUNKSIZE, XPK_Preferences, 0,
  80.       TAG_DONE)))
  81.       {
  82.         if((b.buffer = (STRPTR) AllocVec(b.xfib->xf_NLen, MEMF_ANY)))
  83.         {
  84.           b.filled = 0;
  85.           b.err    = 0;
  86.  
  87.       /* this is a test, saving some really useless stuff */
  88.       {
  89.         ULONG a = *((ULONG *)(((STRPTR) 0x1000000)-20));
  90.  
  91.         SaveData(&b, "IDXTCP10", 8); /* an ID value to detect our data */
  92.         SaveData(&b, "This is the DOSBase:", 21);
  93.         SaveData(&b, DOSBase, sizeof(struct DosLibrary));
  94.         SaveData(&b, "This is the SysBase:", 21);
  95.         SaveData(&b, SysBase, sizeof(struct ExecBase));
  96.         Printf("Storing kickrom, this will take some time\n");
  97.         SaveData(&b, "This is the kickrom:", 21);
  98.         SaveData(&b, ((STRPTR) 0x1000000)-a, a);
  99.         if(err = SaveData(&b, 0, 0))
  100.           XpkPrintFault(err, 0);
  101.       }
  102.           
  103.           FreeVec(b.buffer);
  104.         }
  105.         XpkClose(b.xfib);
  106.       }
  107.       else
  108.         XpkPrintFault(err, 0);
  109.       CloseLibrary(XpkBase);
  110.     }
  111.     FreeArgs(rda);
  112.   }
  113. }
  114.  
  115. /* Call this function for every data to store, calling it with size zero
  116. means all data is done and the last stuff should be stored in file.
  117.  
  118. This function uses a delayed error report, so we need not check every call.
  119. SaveData does nothing except returning the error again, when an error
  120. occured a call before. */
  121. LONG SaveData(struct BufData *b, APTR buf, ULONG size)
  122. {
  123.   LONG i;
  124.  
  125.   if(!size && !b->err) /* end the packed file */
  126.   {
  127.     if(b->filled)
  128.     {
  129.       i = XpkWrite(b->xfib, b->buffer, b->filled);
  130.       b->filled = 0;
  131.       return i;
  132.     }
  133.     else
  134.       return 0;
  135.   }
  136.  
  137.   while(size && !b->err)
  138.   {
  139.     if((i = b->xfib->xf_NLen - b->filled) > size)
  140.       i = size;
  141.  
  142.     CopyMem(buf, b->buffer+b->filled, i);
  143.     b->filled += i;
  144.     size -= i;
  145.     buf = ((STRPTR) buf) + i;
  146.  
  147.     if(b->filled == b->xfib->xf_NLen)
  148.     {
  149.       b->err = XpkWrite(b->xfib, b->buffer, b->filled);
  150.       b->filled = 0;
  151.     }
  152.   }
  153.  
  154.   return b->err;
  155. }
  156.  
  157.